# -*- coding: utf-8 -*-
"""
@Time ： 2021/11/28 20:05
@Auth ： Mr. William 1052949192
@Company ：特斯汀学院 @testingedu.com.cn
@Function ：使用PO封装登录页
"""
from time import sleep

from selenium import webdriver
from selenium.webdriver.common.by import By


class Login:
    """登录页功能封装"""
    def __init__(self):
        self.driver = webdriver.Chrome()
        # 隐式等待：如果立即没找到元素，就等一秒再找，直到找到或者报错（整个浏览器生效）
        self.driver.implicitly_wait(10)

    def login(self,username,pwd):
        """封装登录功能"""
        # 在哪里打开网站？刚打开的浏览器里面打开
        self.driver.get('http://testingedu.com.cn:8000/index.php/Home/user/login.html')

        # selenium定位写法
        ele = self.driver.find_element(By.TAG_NAME, 'input')
        # 输入
        ele.send_keys(username)

        # 得到的是一个元素列表
        eles = self.driver.find_elements(By.TAG_NAME, 'input')
        eles[1].send_keys(pwd)
        # 连写
        self.driver.find_element(By.NAME, 'verify_code').send_keys('1')
        # 点击登录（IE点击无效）
        # driver.find_element(By.CLASS_NAME,'J-login-submit').click()
        ele = self.driver.find_element(By.CLASS_NAME, 'J-login-submit')
        self.driver.execute_script('arguments[0].click()', ele)

    def get_msg(self):
        """获取登录失败的提示"""
        # 获取提示
        sleep(1)
        msg = self.driver.find_element(By.XPATH, '//*[@id="layui-layer1"]/div[2]').text
        print(msg)

    def logout(self):
        """注销登录"""
        sleep(1)
        self.driver.find_element(By.XPATH,'//a[text()="安全退出"]').click()

    def get_nickname(self):
        """获取登录的昵称"""
        sleep(1)
        username = self.driver.find_element(By.XPATH, '//a[@class="red userinfo"]').text
        print(username)


if __name__ == '__main__':
    login = Login()
    # 错误的用户名密码
    login.login('13800138006','11111')
    login.get_msg()
    # 邮箱登录
    login.login('i_wanting@yeah.net','123456')
    login.get_nickname()
    login.logout()

    # 手机号登录
    login.login('13800138006','123456')
    login.get_nickname()

